package vip.ablog.xmall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//
import javax.annotation.Resource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import vip.ablog.common.valid.UpdateStatusGroup;
import vip.ablog.xmall.product.entity.BrandEntity;
import vip.ablog.xmall.product.service.BrandService;
import vip.ablog.common.utils.PageUtils;
import vip.ablog.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author jsp
 * @email jsp@gmail.com
 * @date 2020-08-10 17:11:08
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    ////@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    ////@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    ////@RequiresPermissions("product:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand /*,BindingResult result*/){
//        由统一异常处理
//        if (result.hasErrors()){
//            Map<String,String> map = new HashMap<>(result.getFieldErrors().size());
//            result.getFieldErrors().forEach(item -> {
//                map.put(item.getField(),item.getDefaultMessage());
//            });
//            return R.error().put("data",map);
//        }else {
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // //@RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
    /**
     * 只修改状态，对应另一个校验分组
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
